<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="X 坐标" prop="xCoordinate">
        <el-input
          v-model="queryParams.xCoordinate"
          placeholder="请输入X 坐标"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Y 坐标" prop="yCoordinate">
        <el-input
          v-model="queryParams.yCoordinate"
          placeholder="请输入Y 坐标"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['opiummapcoordinate:opiummapcoordinate:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['opiummapcoordinate:opiummapcoordinate:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['opiummapcoordinate:opiummapcoordinate:remove']"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['opiummapcoordinate:opiummapcoordinate:export']"
        >导出</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="opiummapcoordinateList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="地图ID" align="center" prop="mapId" />
      <el-table-column label="坐标" align="center" prop="xyCoordinate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['opiummapcoordinate:opiummapcoordinate:edit']"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-link"
            @click="getAddressList(scope.row)"
            v-hasPermi="['opiummapcoordinate:opiummapcoordinate:edit']"
          >绑定位置信息</el-button> -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['opiummapcoordinate:opiummapcoordinate:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改禁烟地图坐标对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <div class="tips">提示：鼠标滚轮控制地图缩放，左键选点（三个及以上），右键结束。确定保存</div>
      <div id="container3"></div>
      <!-- <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="逻辑删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="X 坐标" prop="xCoordinate">
          <el-input v-model="form.xCoordinate" placeholder="请输入X 坐标" />
        </el-form-item>
        <el-form-item label="Y 坐标" prop="yCoordinate">
          <el-input v-model="form.yCoordinate" placeholder="请输入Y 坐标" />
        </el-form-item>
        <el-form-item label="禁烟地图ID" prop="mapId">
          <el-input v-model="form.mapId" placeholder="请输入禁烟地图ID" />
        </el-form-item>
      </el-form> -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" style="margin-left:20px">重绘</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" title="选择位置信息" :visible.sync="showAddressList" width="800px" append-to-body>
        <el-table v-loading="loading" :data="opiummapList">
          <el-table-column label="坐标名称" align="center" prop="coordinateName" />
          <el-table-column label="坐标简介" align="center" prop="introduceBriefly" />
          <el-table-column label="区域概况" align="center" prop="regionalOverview" />
          <el-table-column label="区域简介" align="center" prop="regionalProfile" />
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-tag v-if="form.mapId == scope.row.id" effect="dark" type="success">当前绑定地址</el-tag>
              <el-tag v-else effect="dark" type="danger">此地址未选择</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-link"
                @click="chooseAddress(scope.row)"
              >选择</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="addressListTotal>0"
          :total="addressListTotal"
          :page.sync="addressQueryParams.pageNum"
          :limit.sync="addressQueryParams.pageSize"
          @pagination="getAddressList"
        />
    </el-dialog>
  </div>
</template>

<script>
import { listOpiummapcoordinate, getOpiummapcoordinate, delOpiummapcoordinate, addOpiummapcoordinate, updateOpiummapcoordinate, exportOpiummapcoordinate } from "@/api/opiummapcoordinate/opiummapcoordinate";
import { listOpiummap } from "@/api/opiummap/opiummap";

export default {
  name: "Opiummapcoordinate",
  components: {
  },
  data() {
    return {
      map:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 禁烟地图坐标表格数据
      opiummapcoordinateList: [],
      opiummapList:[], // 二级列表
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xCoordinate: null,
        yCoordinate: null,
      },
      addressQueryParams:{
        pageNum: 1,
        pageSize: 10,
      },
      addressListTotal:0,
      showAddressList:false, // 显示选择列表
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询禁烟地址列表 */
    getAddressList(row) {
      this.showAddressList = true
      this.loading = true;
      this.form = row
      listOpiummap(this.addressQueryParams).then(response => {
        this.opiummapList = response.rows;
        this.addressListTotal = response.total;
        this.loading = false;
      });
    },
    mapInit(polygon){
      console.log('地图初始化')
      this.map&&this.map.destory&&this.map.destory()
      this.map = new AMap.Map("container3", {
        center: polygon?polygon[0]:[125.317468,43.890349],
        zoom: 10
      });

    
      this.polyEditor = new AMap.PolygonEditor(this.map);
      if(!polygon){
        this.createPolygon()
      } else {
        var polygon1 = new AMap.Polygon({
          path: polygon
        })
        this.map.add([polygon1]);
        this.map.setFitView();
        this.polyEditor.addAdsorbPolygons([polygon1]);
        this.polyEditor.setTarget(polygon1);
        this.polyEditor.open();
      }
    },
    /** 查询禁烟地图坐标列表 */
    getList() {
      this.loading = true;
      listOpiummapcoordinate(this.queryParams).then(response => {
        console.log(response.rows)
        this.opiummapcoordinateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      // this.open = false;
      // this.reset();
      this.createPolygon()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        xCoordinate: null,
        yCoordinate: null,
        mapId: null
      };
      this.resetForm("form");
    },
    createPolygon() {
        this.map.clearMap()
        this.polyEditor.clearAdsorbPolygons()
        // console.log(polygon._opts.path = [])
        this.polyEditor.close();
        this.polyEditor.setTarget();
        this.polyEditor.open();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加禁烟地图坐标";
      this.$nextTick(()=>{
        this.mapInit()
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      // getOpiummapcoordinate(id).then(response => {
      this.form = row;
      this.open = true;
      this.title = "修改禁烟地图坐标";
      this.$nextTick(()=>{
        this.mapInit(row.xyCoordinateDouble)
      })
      // });
    },
    chooseAddress(row){
      updateOpiummapcoordinate({id:this.form.id,mapId:row.id}).then(response => {
        this.msgSuccess("修改成功");
        this.showAddressList = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      let path = []
      if(this.polyEditor){
        path = this.polyEditor.getTarget()?this.polyEditor.getTarget()._opts.path:[]
      }
      if(path.length == 0){
        this.$message.warning('请选择坐标');
        return
      }
      console.log(JSON.stringify(path))
          if (this.form.id != null) {
            updateOpiummapcoordinate({id:this.form.id,mapId:this.form.mapId,xyCoordinate:JSON.stringify(path)}).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOpiummapcoordinate({xyCoordinate:JSON.stringify(path)}).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除禁烟地图坐标编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOpiummapcoordinate(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有禁烟地图坐标数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOpiummapcoordinate(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style scoped>
#container3{
  width: 100%;
  height: 50vh;
  margin-top: 10px;
}
</style>
